package com.sip.stream.utils.zfy.yuv;

import android.media.Image;
import android.os.SystemClock;
import com.gosuncn.zfyluancher.ZFYLauncherJNI;
import com.sip.stream.utils.ImageYUVBean;

import java.nio.ByteBuffer;

public class I420ImageReader extends ICamera2ImageReader {
    public I420ImageReader(int i, int i2) {
        super(i, i2);
    }

    @Override
    public ImageYUVBean getDataFormImage(Image image) {
        byte[] obtainOutputData = obtainOutputData(this.dataLength);
        ByteBuffer buffer = image.getPlanes()[0].getBuffer();
        ByteBuffer buffer2 = image.getPlanes()[1].getBuffer();
        ByteBuffer buffer3 = image.getPlanes()[2].getBuffer();
        int remaining = buffer2.remaining();
        try {
            buffer.get(obtainOutputData, 0, this.ySize);
            buffer2.get(obtainOutputData, this.ySize, buffer2.remaining());
            buffer3.get(obtainOutputData, this.ySize + remaining, buffer3.remaining());
            byte[] obtainOutputData2 = obtainOutputData(this.dataLength);
            ZFYLauncherJNI.I420ToNV12(obtainOutputData, obtainOutputData2, this.width, this.height);
            ImageYUVBean imageYUVBean = new ImageYUVBean();
            imageYUVBean.setCameraData(obtainOutputData2);
            recycle(obtainOutputData);
            imageYUVBean.setImageTimestampus(SystemClock.elapsedRealtimeNanos() / 1000);
            return imageYUVBean;
        } catch (Exception e) {
            e.printStackTrace();
            recycle(obtainOutputData);
            return null;
        }
    }
}
